package com.example.tourstor;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.example.tourstor.database.DBManager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

public class HomeFragment extends Fragment{

    private LinearLayout layout_plan;
    private LinearLayout layout_friend;
    private LinearLayout layout_funds;

    private ListView routeList;
    private ListAdapter listAdapter;
    private DBManager dbManager = DBManager.createInstance();
    private Handler handler;

    private static final String TAG = "HomeFragment";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        return inflater.inflate(R.layout.frame_home, container);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onActivityCreated: ");
        super.onActivityCreated(savedInstanceState);

        layout_plan = getActivity().findViewById(R.id.layout_home_plan);
        layout_friend = getActivity().findViewById(R.id.layout_home_friend);
        layout_funds = getActivity().findViewById(R.id.layout_home_funds);

        layout_plan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("id", 2);
                startActivity(intent);

            }
        });

        layout_friend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), MainActivity.class);
                intent.putExtra("id", 3);
                startActivity(intent);

            }
        });

        layout_funds.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HelpPlanActivity.class);
                intent.putExtra("id", 3);
                startActivity(intent);
            }
        });




        routeList = getActivity().findViewById(R.id.fragment_home_route);
        new Thread(new Runnable() {
            @Override
            public void run() {

                synchronized (this){
                    dbManager.connectDB();
                    ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
                    try {
                        ResultSet rs = dbManager.stmt.executeQuery("select * from route");
                        while(rs.next()){
                            HashMap<String,String> post=new HashMap<String,String>();
                            post.put("Rid", rs.getString("Rid"));
                            post.put("Rtxt", rs.getString("Rtxt"));
                            list.add(post);
                            Log.i(TAG, "run: Rid = " + rs.getString("Rid"));
                        }
                        rs.close();
                        Log.i(TAG, "run: Select successful");
                    } catch (SQLException e) {
                        Log.i(TAG, "run: SQLException");
                        e.printStackTrace();
                    }
                    dbManager.closeDB();
                    Message msg = handler.obtainMessage(1);
                    msg.what = 1;
                    msg.obj = list;
                    handler.sendMessage(msg);
                }



            }
        }).start();

        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 1){
                    ArrayList<HashMap<String,String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    listAdapter = new SimpleAdapter(getContext(), list, R.layout.item_route,
                            new String[]{"Rid", "Rtxt"},
                            new int[]{R.id.route_id, R.id.route_txt});
                    routeList.setAdapter(listAdapter);



                }




            }
        };


    }
}
